<?php
if(!defined('BZ_XTBLOG')){
    exit('Access Denied!');
}
function template($file) {
	global $tpldir,$tplrefresh,$wappced;
  $fileplugin=explode("#",$file);
  $filep=explode("/",$file);
  if($fileplugin[1]){
		 $tplfile = MBLOG_ROOT.'source/plugin/'.$fileplugin[1].'/templates/'.$fileplugin[2];
	   $objfile = MBLOG_ROOT.'cache/templates/'.$fileplugin[1].'/'.$fileplugin[2].'.tpl.php';
     if(!file_exists('cache/templates/'.$fileplugin[1].'/')){
         @mkdir('cache/templates/'.$fileplugin[1].'/',0777,true);
     }
  }else{
     if($filep[1]){
		    $tplfile = MBLOG_ROOT.$tpldir.'/'.$filep[0].'/'.$filep[1].'.htm';
	      $objfile = MBLOG_ROOT.'cache/templates/'.$filep[0].'/'.$wappced.'_'.$filep[1].'.tpl.php';
        if(!file_exists('cache/templates/'.$filep[0].'/')){
            @mkdir('cache/templates/'.$filep[0].'/',0777,true);
        }
	   }else{
		    $tplfile = MBLOG_ROOT.$tpldir.'/'.$file.'.htm';
	      $objfile = MBLOG_ROOT.'cache/templates/'.$wappced.'_'.$file.'.tpl.php';
     }
  }  
    if($tplrefresh == '0'){//即时
        @unlink ("$objfile");
    }
    if(abs(filesize($objfile)) < 50){
        @unlink ("$objfile");
    }
    if(!file_exists($objfile)) {
        parse_template($tplfile,$objfile);
    }
    return $objfile;
}
function checktplrefresh($tplfile, $objfile, $timecompare) {
    global $tplrefresh,$timeout;
    if($tplrefresh == '-1'){//不
        $adhjcrer=filemtime("$tplfile");
    	  if($adhjcrer > $timecompare ){
           parse_template($tplfile,$objfile);
           return TRUE;
        }
    } else if($tplrefresh == '0'){//即时
           parse_template($tplfile,$objfile);
           return TRUE;
    } else if($tplrefresh > '0'){//定时
    	  if((time()-$timecompare) > $tplrefresh){
           parse_template($tplfile,$objfile);
           return TRUE;
        }
    }
	return FALSE;
}

function parse_template($tplfile,$objfile) {
    global $tpldir;
  if(!$fp = @fopen($tplfile, 'r')) {
  	  $ssid=explode(MBLOG_ROOT,$tplfile);
		  echo "Current template file '$ssid[1]' not found or have no access!</br>";
      Bingz_Error_Reporting('Current template file '.$ssid[1].' not found or have no access!','插件'); 
	} else {
	    @$template = fread($fp, filesize($tplfile));
	    fclose($fp);
	
    for($i = 1; $i < 4; $i ++) {
        $template = preg_replace("/[\n\r\t]*\{subtemplate\s+(.+?)\}[\n\r\t]*/is", "<?php @include template('\\1'); ?>", $template);
    }
	$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
	$template = preg_replace("/\{(.+?)\}/s", "{\\1}", $template);
	$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
	$template = preg_replace("/\{lang\s+(.+?)\}/ies", "languagevar('\\1')", $template);
  $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", $template);
	$template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/is", "<?php \\1; ?>", $template);
  $template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/is", "<?php echo \\1; ?>", $template);
	$template = preg_replace("/([\n\r\t]*)\{elseif\s+(.+?)\}([\n\r\t]*)/is", "\\1<?php } elseif(\\2) { ?>\\3", $template);
	$template = preg_replace("/([\n\r\t]*)\{else if\s+(.+?)\}([\n\r\t]*)/is", "\\1<?php } elseif(\\2) { ?>\\3", $template);
	$template = preg_replace("/([\n\r\t]*)\{else\}([\n\r\t]*)/is", "\\1<?php } else { ?>\\2", $template);
  $template = preg_replace("/[\n\r\t]*\{print_r\s+(.+?)\}[\n\r\t]*/is", "<?php print_r(\\1); ?>", $template);
	for($i = 0; $i < 6; $i++) {
     $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/is", "<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>\\3<?php } } ?>", $template);
     $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/is", "<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>\\4<?php } } ?>", $template);
		 $template = preg_replace("/([\n\r\t]*)\{if\s+(.+?)\}([\n\r]*)(.+?)([\n\r]*)\{\/if\}([\n\r\t]*)/is", "\\1<?php if(\\2) { ?>\\3\\4\\5<?php } ?>\\6", $template);
	}
  $template = preg_replace("/\{([a-zA-Z_\x7f-\xff] [a-zA-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1; ?>", $template);
	$template = preg_replace("/ \?\>[\n\r]*\<\?php /s", " ", $template);
	if($fp = @fopen($objfile, 'w')) {
    $template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "transamp('\\0')", $template);
	  $template = preg_replace("/\<script[^\>]*?src=\"(.+?)\"(.*?)\>\s*\<\/script\>/ise", "stripscriptamp('\\1', '\\2')", $template);
    $template = "<?php if(!defined('BZ_XTBLOG')) exit('Access Denied'); checktplrefresh('$tplfile','$objfile', ".time()."); ?>\n$template";
    flock($fp, 2);
	  fwrite($fp, $template);
	  fclose($fp);
	}
}
}

function transamp($str) {
	$str = str_replace('&', '&amp;', $str);
	$str = str_replace('&amp;amp;', '&amp;', $str);
	$str = str_replace('\"', '"', $str);
	return $str;
}

function languagevar($var) {
	global $languagext,$language;
	if(isset($languagext[$var])) {
		return $languagext[$var];
	} else if(isset($language[$var])) {
		return $language[$var];
	} else {
		return "!$var!";
	}
}
function stripscriptamp($s, $extra) {
	$extra = str_replace('\\"', '"', $extra);
	$s = str_replace('&amp;', '&', $s);
	return "<script src=\"$s\" type=\"text/javascript\"$extra></script>";
}
?>
